/**
 * 
 */
package com.google.code.cisx.client.gadgets;

import com.google.code.cisx.client.event.EventManager;
import com.google.code.cisx.client.gadgets.GadgetEvent.GadgetEventType;
import com.google.code.cisx.client.login.LoginEvent;
import com.google.code.cisx.client.login.LoginPanel;
import com.google.code.cisx.client.login.LoginEvent.LoginEventType;
import com.google.code.cisx.client.login.service.LoginService;
import com.google.code.cisx.client.model.Gadget;
import com.google.code.cisx.client.model.GadgetClass;
import com.google.code.cisx.client.session.Session;
import com.google.code.cisx.client.session.SessionEvent;
import com.google.code.cisx.client.session.SessionTracker;
import com.google.code.cisx.client.session.SessionEvent.SessionEventType;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @since 1.0
 * @version $Id: LoginGadget.java 258 2008-11-29 06:23:51Z rahul.thakur.xdev $
 */
public class LoginGadget extends Gadget {

  public static class Class extends GadgetClass {

    public Class() {
      super("Login", true, false /* false implies not publically visible */);
    }

    public Gadget newGadget() {
      return new LoginGadget(this);
    }
  }

  private final LoginPanel.LoginListener loginListener = new LoginPanel.LoginListener() {

    public void onSubmit(final LoginPanel loginPanel) {

      // clear any message on the Login Panel
      loginPanel.setMessage("");
      loginPanel.setErrorMessage("");

      LoginService.Util.getAsyncInstance().login(loginPanel.getUsername(),
          loginPanel.getPassword(), new AsyncCallback() {

            public void onFailure(Throwable caught) {
              loginPanel.setErrorMessage("System error! Please try again later.");
            }

            public void onSuccess(Object result) {
              if (result instanceof String) {
                if (((String) result).equals("Success")) {
                  EventManager.getInstance().dispatchEvent(
                      new LoginEvent(loginPanel, LoginEventType.LOGIN.toString()));
                  Session newSession = Session.newSession();
                  SessionTracker.getInstance().bindSession(newSession);
                  loginPanel.setMessage("Login Successful!");

                  EventManager.getInstance().dispatchEvent(
                      new SessionEvent(LoginGadget.this, SessionEventType.LOGIN.toString()));

                  EventManager.getInstance().dispatchEvent(
                      new GadgetEvent(LoginGadget.this, GadgetEventType.GADGET_CLOSED.toString()));
                } else {
                  EventManager.getInstance().dispatchEvent(
                      new LoginEvent(loginPanel, LoginEventType.LOGOUT.toString()));
                  loginPanel.setErrorMessage("Login Failed!");
                }
              }
              // else do nothing
            }
          });
    }

  };

  private final LoginPanel loginPanel = new LoginPanel(loginListener);

  /**
   * @param gc
   */
  protected LoginGadget(Class gc) {
    super(gc);
    HorizontalPanel mpanel = new HorizontalPanel();
    initWidget(mpanel);
    mpanel.setWidth("100%");
    mpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    mpanel.add(loginPanel);
    loginPanel.setStyleName("Login");
  }

}
